package com.Txunda.fxw.ui.adapter.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.Txunda.fxw.ui.listener.AdapterViewClickListener;
import com.Txunda.fxw.ui.listener.OnItemClickListener;
import com.Txunda.fxw.ui.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间：2017/12/20 16:07
 * 作    者：Hzj
 * 描    述：所有RecycerView的父Adapter
 * 修    改：
 **/
public abstract class BaseRecyclerViewAdapter<T, VH extends BaseRecyclerViewHolder<T>> extends RecyclerView.Adapter<VH> {

    /**
     * RecyclerView点击事件
     */
    private OnItemClickListener<T> mOnItemClickListener;
    /**
     * RecyclerView长按事件
     */
    private OnItemLongClickListener<T> mOnItemLongClickListener;
    /**
     * item布局中子控件的点击事件
     */
    private AdapterViewClickListener<T> adapterViewClickListener;
    /**
     * RecyclerView要展示的数据
     */
    private List<T> mList = new ArrayList<>();

    protected Context mContext;


    public BaseRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }

    /**
     * 更新数据
     *
     * @param mDatas 新增的数据
     */
    public void update(List<T> mDatas) {
        this.mList.addAll(mDatas);
        notifyDataSetChanged();
    }

    /**
     * 清除掉所有数据
     */
    public void clear() {
        this.mList.clear();
        notifyDataSetChanged();
    }

    /**
     * 获取单挑数据
     *
     * @param position 下标
     * @return T
     */
    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * 获取全部数据
     *
     * @return List
     */
    public List<T> getDates() {
        return mList;
    }

    /**
     * 设置长按监听事件
     *
     * @param onItemLongClickListener 长按事件
     */
    public void setOnItemLongClickListener(OnItemLongClickListener<T> onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    /**
     * 点击事件
     *
     * @param onItemClickListener 点击事件
     */
    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    /**
     * 控件的点击事件
     *
     * @param adapterViewClickListener 点击事件
     */
    public void setAdapterViewClickListener(AdapterViewClickListener<T> adapterViewClickListener) {
        this.adapterViewClickListener = adapterViewClickListener;
    }

    /**
     * inflate a view by viewGroup ,id ,etc
     *
     * @param viewGroup ViewGroup
     * @param layoutId  布局id
     * @return View
     */
    protected View inflateItemView(ViewGroup viewGroup, int layoutId) {
        return inflateItemView(viewGroup, layoutId, false);
    }

    /**
     * inflate a view by viewGroup ,id ,etc
     *
     * @param viewGroup ViewGroup
     * @param layoutId  布局id
     * @param attach    attach
     * @return View
     */
    protected View inflateItemView(ViewGroup viewGroup, int layoutId, boolean attach) {
        return LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup, attach);
    }


    /**
     * a final function to avoid you override
     * use template design pattern
     *
     * @param vh       ViewHolder
     * @param position 下标
     */
    @Override
    public final void onBindViewHolder(@NonNull VH vh, int position) {
        final T item = getItem(position);
        bindDataToItemView(vh, position, item);
        bindItemViewClickListener(vh, position, item);
    }


    /**
     * 给每个item设置对应的值
     *
     * @param vh   viewHolder
     * @param item item
     */
    private void bindDataToItemView(VH vh, int position, T item) {
        vh.setData(item, position);
        if (adapterViewClickListener != null) {
            vh.setAdapterViewClickListener(adapterViewClickListener);
            vh.setViewOnClick(position, item);
        }
    }


    /**
     * 给itemView绑定点击事件或者长按事件
     *
     * @param vh       VH
     * @param position item下标
     * @param item     item  T
     */
    private void bindItemViewClickListener(VH vh, int position, T item) {
        if (mOnItemClickListener != null) {// item点击事件
            vh.setOnItemClickListener(mOnItemClickListener);
            vh.bindItemClickListener(position, item);
        }
        if (mOnItemLongClickListener != null) {// item长按事件
            vh.setOnItemLongClickListener(mOnItemLongClickListener);
            vh.bindItemClickListener(position, item);
        }
    }

}
